package com.muyu.payment.controller;

import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.payment.domain.PaymentDict;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.payment.domain.PaymentOrder;
import com.muyu.payment.domain.req.PaymentOrderQueryReq;
import com.muyu.payment.domain.model.PaymentOrderQueryModel;
import com.muyu.payment.domain.model.PaymentOrderPageQueryModel;
import com.muyu.payment.domain.resp.PaymentOrderPageResp;
import com.muyu.payment.domain.req.PaymentOrderSaveReq;
import com.muyu.payment.domain.req.PaymentOrderEditReq;
import com.muyu.payment.domain.model.PaymentOrderSaveModel;
import com.muyu.payment.domain.model.PaymentOrderEditModel;
import com.muyu.payment.service.PaymentOrderService;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;

/**
 * 支付订单Controller
 *
 * @author muyu
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/order")
@Tag(name = "支付订单", description = "支付订单控制层")
public class PaymentOrderController extends BaseController {

    /**
    * 支付订单业务层
    */
    @Autowired
    private PaymentOrderService paymentOrderService;

    /**
     * 查询支付订单列表
     */
    @Operation(summary = "获取支付订单列表", description = "获取支付订单列表")
    @GetMapping("/list")
    public Result<DataPageResp<PaymentOrderPageResp>> list(PaymentOrderQueryReq paymentOrderQueryReq) {
        PageQueryModel<PaymentOrderPageQueryModel> pageQueryModel = paymentOrderService.pageQuery(PaymentOrderQueryModel.reqBuild(paymentOrderQueryReq));
        return Result.success(DataPageResp.of(pageQueryModel).cover(PaymentOrderPageResp::infoBuild));
    }

    /**
     * 导出支付订单列表
     */
    @Operation(summary = "导出支付订单列表", description = "导出支付订单列表")
    @Log(title = "支付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentOrder paymentOrder) {
        List<PaymentOrder> list = paymentOrderService.exportList(paymentOrder);
        ExcelUtil<PaymentOrder> util = new ExcelUtil<>(PaymentOrder.class);
        util.exportExcel(response, list, "支付订单数据");
    }

    /**
     * 获取支付订单详细信息
     */
    @Operation(summary = "获取支付订单详细信息", description = "获取支付订单详细信息")
    @GetMapping(value = "/{id}")
    public Result<PaymentOrder> getInfo(@PathVariable("id") String id) {
        return Result.success(paymentOrderService.getById(id));
    }

    /**
     * 新增支付订单
     */
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增支付订单", description = "新增支付订单")
    public Result<String> add(@RequestBody PaymentOrderSaveReq paymentOrderSaveReq) {
        paymentOrderService.save(PaymentOrderSaveModel.saveReqBuild(paymentOrderSaveReq));
        return Result.success();
    }

    /**
     * 修改支付订单
     */
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @Operation(summary = "修改支付订单", description = "修改支付订单")
    public Result<String> edit(@PathVariable(value = "id") Long id, @RequestBody PaymentOrderEditReq paymentOrderEditReq) {
        paymentOrderService.edit(PaymentOrderEditModel.editReqBuild(id,paymentOrderEditReq));
        return Result.success();
    }

    /**
     * 删除支付订单
     */
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Operation(summary = "删除支付订单", description = "删除支付订单")
    public Result<String> remove(@PathVariable(value = "ids") List<String> ids) {
        return toAjax(paymentOrderService.removeBatchByIds(ids));
    }

    /**
     * 获取字典类型数据
     * @param dictType
     * @return
     */
    @Operation(summary = "获取字典类型数据" , description = "获取字典类型数据")
    @GetMapping("/dict/{dictType}")
    public Result<List<PaymentDict>> dict(@PathVariable("dictType") String dictType) {
        List<PaymentDict> list = paymentOrderService.dict(dictType);
        return Result.success(list);
    }


}
